/****************************************************************
  This is to plot the elas from baseline model and other models
  in the same graph.
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

log using log_MSM_elas_compare.log, replace;

global gpicsdir "pics_elas_compare";
global gdtadir ".";


do do_globals.do;

* from the baseline model;
use ${gdtadir}/dta_MSM_elas_calculated.dta, clear;
gen byte model = 0;
save ${gpicsdir}/dta_MSM_elas_compare.dta, replace;

* exogenous model and learning by doing;
foreach iv in 1 2 {;
   use ../../../BP_MPI_${gv_alt`iv'}/main/output/dta_MSM_elas_calculated.dta, clear;
   gen byte model =  ${gv_alt`iv'_id};
   append using ${gpicsdir}/dta_MSM_elas_compare.dta;
   sort model t;
   save ${gpicsdir}/dta_MSM_elas_compare.dta, replace;
};


* plot graph;
local lvelas me0 me2 ies0 ies2;
keep model t `lvelas';
foreach iv of local lvelas {;
   replace `iv' = . if `iv'<=0;
}; 

reshape wide me0 me2 ies0 ies2, i(t) j(model);
sort t;

local lvlastT = 70;

local lvxx me2${gv_alt1_id} ies2${gv_alt1_id} me0${gv_alt1_id} ies0${gv_alt1_id};
foreach iv in `lvxx' {;
   replace `iv'=. if t>80;
};


* comparison table;
export excel t me20 ies20 me2${gv_alt1_id} ies2${gv_alt1_id} me2${gv_alt2_id} ies2${gv_alt2_id} using "table4paper", sheet("elas_alt_dlnw") sheetreplace firstrow(variables);
export excel t me00 ies00 me0${gv_alt1_id} ies0${gv_alt1_id} me0${gv_alt2_id} ies0${gv_alt2_id} using "table4paper", sheet("elas_alt_dH") sheetreplace firstrow(variables);

preserve;
   keep if mod(t,5)==0;
   sort t;
   export excel t me20 ies20 me2${gv_alt1_id} ies2${gv_alt1_id} me2${gv_alt2_id} ies2${gv_alt2_id} using "table4paper", sheet("elas_alt_dlnw_5") sheetreplace firstrow(variables);
   export excel t me00 ies00 me0${gv_alt1_id} ies0${gv_alt1_id} me0${gv_alt2_id} ies0${gv_alt2_id} using "table4paper", sheet("elas_alt_dH_5") sheetreplace firstrow(variables);
restore;

local lvxlabel "20 30(10)60 `lvlastT'";
local lvcond "t>=20 & t<=`lvlastT'";

sort t;
foreach iv in 2 {;

   line me`iv'0 me`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel',grid) ytitle("Elasticities")
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare2_me`iv'.eps, replace;
   line ies`iv'0 ies`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel',grid) ytitle("IES")
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare2_ies`iv'.eps, replace;


   line me`iv'0 me`iv'${gv_alt1_id} me`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("Elasticities") 
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt1_des}" 3 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare3_me`iv'.eps, replace;
   line me`iv'0 me`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("Elasticities")
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare2_me`iv'.eps, replace;
   line ies`iv'0 ies`iv'${gv_alt1_id} ies`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("IES")
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt1_des}" 3 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare3_ies`iv'.eps, replace;

   * log scale in the y-axis;
   line me`iv'0 me`iv'${gv_alt1_id} me`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("Elasticities") 
          yscale(log range(0.1 7)) ylabel(0.1 0.2 0.3 0.4 0.6 1 2 4 6)
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt1_des}" 3 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare3_me`iv'_log.eps, replace;
   line me`iv'0 me`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("Elasticities") 
          yscale(log range(0.1 7)) ylabel(0.1 0.2 0.3 0.4 0.6 1 2 4 6)
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare2_me`iv'_log.eps, replace;


      local lvylabel 0.1 0.2 0.3 0.5 1 2 4 8;
      local lvyrange 0.1 8;
   
   line ies`iv'0 ies`iv'${gv_alt1_id} ies`iv'${gv_alt2_id} t if `lvcond', lpattern("l" "_" "-" "-.") graphregion(color(white))
          xtitle("Age") xlabel(`lvxlabel', grid) ytitle("IES") 
          yscale(log range(`lvyrange')) ylabel(`lvylabel')
          legend(pos(12) ring(0) col(1) colgap(*0.25) symxsize(*1) order(1 "Baseline" 2 "${gv_alt1_des}" 3 "${gv_alt2_des}"));
   graph export ${gpicsdir}/eps_elas_compare3_ies`iv'_log.eps, replace;

};


cap log close;


